'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
  async up (queryInterface, Sequelize) {
    await queryInterface.createTable('sys_layer', {
      id: {
        type: Sequelize.BIGINT,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true,
        comment: "ID"
      },
      menu_id: {
        type: Sequelize.BIGINT,
        allowNull: false,
        comment: "关联的菜单ID"
      },
      layer_name: {
        type: Sequelize.CHAR,
        allowNull: false,
        comment: "图层名称"
      },
      layer_type: {
        type: Sequelize.CHAR,
        allowNull: false,
        comment: "图层类型（如：WMS、WFS、Vector等）"
      },
      layer_url: {
        type: Sequelize.CHAR,
        allowNull: false,
        comment: "图层服务地址"
      },
      layer_style: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: "图层样式配置"
      },
      layer_params: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: "图层参数配置"
      },
      is_visible: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 1,
        comment: "是否可见（1-是 0-否）"
      },
      order_num: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: "显示顺序"
      },
      status: {
        type: Sequelize.CHAR,
        allowNull: false,
        defaultValue: "0",
        comment: "状态（0正常 1停用）"
      },
      del_flag: {
        type: Sequelize.CHAR,
        allowNull: false,
        defaultValue: "0",
        comment: "删除标志（0代表存在 2代表删除）"
      },
      create_by: {
        type: Sequelize.CHAR,
        allowNull: false,
        defaultValue: "",
        comment: "创建者"
      },
      create_time: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: "创建时间"
      },
      update_by: {
        type: Sequelize.CHAR,
        allowNull: false,
        defaultValue: "",
        comment: "更新者"
      },
      update_time: {
        type: Sequelize.DATE,
        allowNull: true,
        comment: "更新时间"
      },
      remark: {
        type: Sequelize.CHAR,
        allowNull: true,
        comment: "备注"
      }
    });

    // 添加外键约束
    await queryInterface.addConstraint('sys_layer', {
      fields: ['menu_id'],
      type: 'foreign key',
      name: 'fk_sys_layer_menu_id',
      references: {
        table: 'sys_menu',
        field: 'id'
      },
      onDelete: 'CASCADE',
      onUpdate: 'CASCADE'
    });
  },

  async down (queryInterface, Sequelize) {
    await queryInterface.dropTable('sys_layer');
  }
};